package com.ruoyi.carowner.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.carowner.domain.CarownerKm;
import com.ruoyi.carowner.service.ICarownerKmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 里程Controller
 *
 * @author ruoyi
 * @date 2024-08-15
 */
@RestController
@RequestMapping("/carowner/km")
public class CarownerKmController extends BaseController {
    @Autowired
    private ICarownerKmService carownerKmService;

    /**
     * 查询里程列表
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarownerKm carownerKm) {
        startPage();
        List<CarownerKm> list = carownerKmService.selectCarownerKmList(carownerKm);
        return getDataTable(list);
    }

    @GetMapping("/getByUserId/{userId}")
    public AjaxResult getByUserId(@PathVariable("userId") Integer userId) {
        return success(carownerKmService.selectKmByUserid(userId));
    }
    /**
     * 导出里程列表
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:export')")
    @Log(title = "里程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarownerKm carownerKm) {
        List<CarownerKm> list = carownerKmService.selectCarownerKmList(carownerKm);
        ExcelUtil<CarownerKm> util = new ExcelUtil<CarownerKm>(CarownerKm.class);
        util.exportExcel(response, list, "里程数据");
    }

    /**
     * 获取里程详细信息
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(carownerKmService.selectCarownerKmById(id));
    }




    /**
     * 新增里程
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:add')")
    @Log(title = "里程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarownerKm carownerKm) {
        return toAjax(carownerKmService.insertCarownerKm(carownerKm));
    }

    /**
     * 修改里程
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:edit')")
    @Log(title = "里程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarownerKm carownerKm) {
        return toAjax(carownerKmService.updateCarownerKm(carownerKm));
    }

    /**
     * 删除里程
     */
    @PreAuthorize("@ss.hasPermi('carowner:km:remove')")
    @Log(title = "里程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(carownerKmService.deleteCarownerKmByIds(ids));
    }
}
